package com.webwork.project.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.webwork.common.dao.ColumnLimit;
import com.webwork.common.dao.DBSimpleSQL;
import com.webwork.common.dao.SqlCondition;
import com.webwork.common.dao.impl.SimpleDao;
import com.webwork.project.system.dao.IQueryUserDao;
import com.webwork.project.system.dao.IUpdateUserDao;
import com.webwork.project.system.dataobject.SysUser;
import com.webwork.project.system.dataobject.SysUserQuery;

/**
 * 用户Dao实现
 *
 */
@Repository
public class UserDaoImpl extends SimpleDao implements IQueryUserDao, IUpdateUserDao {

	public UserDaoImpl() {
		super();
		this.dbTable = "system_user";
		dbColumns.clear();
		dbColumns.put("id", "id");
		dbColumns.put("userAccount", "user_account");
		dbColumns.put("userName", "user_name");
		dbColumns.put("password", "password");
		dbColumns.put("sex", "sex");
		dbColumns.put("phone", "phone");
		dbColumns.put("email", "email");
		dbColumns.put("weixin", "weixin");
		dbColumns.put("jobId", "job_id");
		dbColumns.put("createTime", "create_time");
		dbColumns.put("modifyTime", "modify_time");
		dbColumns.put("status", "user_status");
	}
	
	@Override
	public ColumnLimit condition(Object obj) {
		if (!(obj instanceof SysUserQuery))
			return new ColumnLimit();
		SysUserQuery o = (SysUserQuery) obj;
		ColumnLimit ret = new ColumnLimit();
		if (null != o) {
			if (!StringUtils.isEmpty(o.getUserAccount())) {
				ret.add(dbColumns.get("userAccount").concat(SqlCondition.EQ));
			}
		}
		return ret;
	}

	@Override
	public Object[] statement(Object obj) {
		if (!(obj instanceof SysUserQuery))
			return null;
		SysUserQuery o = (SysUserQuery) obj;
		List<Object> ret = new ArrayList<Object>();
		if (null != o) {
			if (!StringUtils.isEmpty(o.getUserAccount())) {
				ret.add(o.getUserAccount());
			}
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		SysUser o = (SysUser) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getId());
		objs.add(o.getUserAccount());
		objs.add(o.getUserName());
		objs.add(o.getPassword());
		objs.add(o.getSex());
		objs.add(o.getPhone());
		objs.add(o.getEmail());
		objs.add(o.getWeixin());
		objs.add(o.getJobId());
		objs.add(o.getCreateTime());
		objs.add(o.getModifyTime());
		objs.add(o.getStatus().getValue());
		return objs;
	}

	@Override
	public Object catchObject(ResultSet rs) throws SQLException {
		SysUser ret = new SysUser();
		ret.setId(rs.getInt(dbColumns.get("id")));
		ret.setUserAccount(rs.getString(dbColumns.get("userAccount")));
		ret.setUserName(rs.getString(dbColumns.get("userName")));
		ret.setPassword(rs.getString(dbColumns.get("password")));
		ret.setSex(rs.getInt(dbColumns.get("sex")));
		ret.setPhone(rs.getString(dbColumns.get("phone")));
		ret.setEmail(rs.getString(dbColumns.get("email")));
		ret.setWeixin(rs.getString(dbColumns.get("weixin")));
		ret.setJobId(rs.getInt(dbColumns.get("jobId")));
		ret.setCreateTime(rs.getDate(dbColumns.get("createTime")));
		ret.setModifyTime(rs.getDate(dbColumns.get("modifyTime")));
		ret.setStatus(rs.getInt(dbColumns.get("status")));
		return ret;
	}
	
	@Override
	public int insert(SysUser o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		return this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public int update(SysUser o, int id) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("id").concat(SqlCondition.EQ));
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(id);
		return this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public int delete(int id) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("id").concat(SqlCondition.EQ));
		String sql = DBSimpleSQL.del(dbTable, limit);
		return this.getJdbcTemplate().update(sql, id);
	}

	@Override
	public List<SysUser> query(SysUserQuery obj, int page, int size) {
		List<SysUser> ret = new ArrayList<SysUser>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(obj), page, size);
		ret = this.getJdbcTemplate().query(sql, new RowMapper<SysUser>() {
			@Override
			public SysUser mapRow(ResultSet rs, int rowNum) throws SQLException {
				return (SysUser) catchObject(rs);
			}
		}, this.statement(obj));
		return ret;
	}

	@Override
	public int count(SysUserQuery obj) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(obj));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(obj), Integer.class);
	}
	
}
